******************************************************************************************************************
*		
*		Tile: Political Agency, Election Quality, and Corruption
*
*		Authors: Nelson Ruiz-Guarin and Miguel R. Rueda
* 
*		Description: It generates all results of paper and appendix and figures with the exception of Table 2 in appendix and Figure 3 in the appendix
*
******************************************************************************************************************

use "C:\Users\mrueda\Documents\Emory\Papers\Corruption\Data\Colombia\replication_data.dta", clear
set more off


*Table 1*
tsset muni_code year

foreach var in alcalde alcalde_guilty alcalde_impeached{
	reg `var' l_vb_r  sanc_before   l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education  llpopulation, cluster(muni_code)
	xtreg `var' l_vb_r sanc_before  l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation, fe cluster(muni_code)
}

*Table 2*
foreach var in alcalde alcalde_guilty alcalde_impeached{
	ivreg2 `var' (l_vb_r=lm_pob_mesa) sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation llpotencial,cluster(muni_code) ffirst
	reg l_vb_r lm_pob_mesa sanc_before l4.margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation llpotencial  if e(sample),cluster(muni_code)
}

foreach var in alcalde alcalde_guilty alcalde_impeached {	
	ivreg2 `var' (l_vb_r=lz_pob_mesa) lpotencial , first cluster(muni_code)
	ivreg2 `var' (l_vb_r=lz_pob_mesa) lpotencial  if abs(forcing)<=40,first cluster(muni_code)
	}


*Table 3*
reg l_vb_r wage_mayor_upper_mw l4.margin_index2 l.armed_actor l.rural_pop_t l.q_education lown_resources l.lpopulation  if local_e==1, cluster(muni_code)
xtreg l_vb_r wage_mayor_upper_mw l4.margin_index2 l.armed_actor l.rural_pop_t l.q_education lown_resources l.lpopulation   if local_e==1, fe cluster(muni_code)
reg l_vb_r ly_mw l4.margin_index2 l.armed_actor l.rural_pop_t l.q_education lown_resources l.lpopulation  if local_e==1, cluster(muni_code)
xtreg l_vb_r ly_mw l4.margin_index2 l.armed_actor l.rural_pop_t l.q_education lown_resources l.lpopulation  if local_e==1, fe cluster(muni_code)
	
	
*Table 4*
foreach var in alcalde alcalde_guilty alcalde_impeached{
foreach var2 in ly_mw {
	reg `var' `var2' sanc_before l4.margin_index2 l.armed_actor l.rural_pop_t lown_resources l.q_education l.lpopulation if l_vb_r>0, cluster(muni_code)
	
	test `var2'
	di 1-ttail(r(df_r),sign(_b[`var2'])*sqrt(r(F)))
	
	xtreg `var' `var2'  sanc_before l4.margin_index2 l.armed_actor l.rural_pop_t lown_resources l.q_education l.lpopulation if l_vb_r>0, fe cluster(muni_code)
	
	test `var2'
	di 1-ttail(r(df_r),sign(_b[`var2'])*sqrt(r(F)))
}
}

*Figure 1*
twoway (line z_pob_mesa_f potencial if potencial<5000&year==2007, sort legend(on)) (line m_pob_mesa potencial if potencial<5000&year==2007, sort)

	
*Figure 2*
	
xtreg alcalde_guilty c.l_vb_r##runs_nelec_same_tipo sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(runs_nelec_same_tipo==0) at(runs_nelec_same_tipo==1) 
	marginsplot,recast(scatter) xscale(range(0.5 2.5))

xtreg alcalde_impeached c.l_vb_r##runs_nelec_same_tipo sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(runs_nelec_same_tipo==0) at(runs_nelec_same_tipo==1) 
	marginsplot,recast(scatter) xscale(range(0.5 2.5))

	
***********Appendix********************	
	
**Figure 1 
rddensity forcing if (year == 2003 | year == 2007),c(0) plot
	
**Figure 2 
semipar ITM06  sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education  llpopulation if l_vb_r>0, nonpar(ly_mw) degree(3) ci robust
semipar visibility  sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education  llpopulation if l_vb_r>0, nonpar(ly_mw) degree(3) ci robust
semipar performance  sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education  llpopulation if l_vb_r>0, nonpar(ly_mw) ci robust degree(3)
semipar participation  sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education  llpopulation if l_vb_r>0, nonpar(ly_mw) degree(3) ci robust

**Figure 4 
semipar alcalde sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation if l_vb_r>0, nonpar(ly_mw2) ci cluster(muni_code) 
semipar alcalde_guilty sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation if l_vb_r>0, nonpar(ly_mw2) ci cluster(muni_code) 
semipar alcalde_impeached sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation if l_vb_r>0, nonpar(ly_mw2) ci cluster(muni_code) 
	
	
**Table 3 
*Same municipality next possible election
quietly: xtreg alcalde c.l_vb_r##runs_nelec_same_tipo sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(runs_nelec_same_tipo==0) at(runs_nelec_same_tipo==1) 
quietly: xtreg alcalde_guilty c.l_vb_r##runs_nelec_same_tipo sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(runs_nelec_same_tipo==0) at(runs_nelec_same_tipo==1) 
quietly: xtreg alcalde_impeached c.l_vb_r##runs_nelec_same_tipo sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(runs_nelec_same_tipo==0) at(runs_nelec_same_tipo==1) 

*Alcalde in same municipality
quietly: xtreg alcalde c.l_vb_r##i.d_alcal_same sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(d_alcal_same==0) at(d_alcal_same==1) 
quietly: xtreg alcalde_guilty c.l_vb_r##i.d_alcal_same sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(d_alcal_same==0) at(d_alcal_same==1) 
quietly: xtreg alcalde_impeached c.l_vb_r##i.d_alcal_same sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(d_alcal_same==0) at(d_alcal_same==1) 

*Index
quietly: xtreg alcalde c.l_vb_r##c.index sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(index==0) at(index==1) 
quietly: xtreg alcalde_guilty c.l_vb_r##c.index sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation  ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(index==0) at(index==1) 
quietly: xtreg alcalde_impeached c.l_vb_r##c.index sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education llpopulation ,fe cluster(muni_code)	
margins, dydx(l_vb_r) at(index==0) at(index==1) 

		
**Table 4 
foreach var in visibility performance participation ITM06{
reg `var' l_vb_r sanc_before l4.margin_index2 l.armed_actor l.rural_pop_t lown_resources l.q_education  l.lpopulation, robust
}

**Table 5 
quietly: reg alcalde l_vb_r l4.margin_index2 l.armed_actor l.rural_pop_t lown_resources l.q_education lpopulation 
sum alcalde alcalde_guilty alcalde_impeached VB_pc y_mw wage_mayor_upper_mw l.armed_actor l.q_education l4.margin_index2 lown_resources population z_pob_mesa pob_mesa l.rural_pop_t sanc_before if e(sample)
sum ITM06

**Table 6 
foreach var in alcalde alcalde_guilty alcalde_impeached{
	logit `var' l_vb_r sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education  llpopulation  , cluster(muni_code)
}
foreach var in alcalde alcalde_guilty alcalde_impeached{
	logit `var' ly_mw sanc_before l4margin_index2 larmed_actor lrural_pop_t lown_resources lq_education  llpopulation if l_vb_r>0, cluster(muni_code)
}




	
